package com.zhida.jiyangcenter.modle

import android.os.Bundle
import android.util.SparseArray
import androidx.appcompat.widget.AppCompatImageView
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import chooongg.base.annotation.ContentLayout
import chooongg.base.annotation.TitleBar
import chooongg.base.utils.tipFail
import chooongg.base.widget.recyclerView.divider.HorizontalDividerItemDecoration
import chooongg.http.APIException
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.zhida.jiyangcenter.R
import com.zhida.jiyangcenter.api.APIZaxiang
import com.zhida.jiyangcenter.api.BaseAPI
import com.zhida.jiyangcenter.api.DoctorServeInfoEntity
import com.zhida.jiyangcenter.mian.BaseActivity
import com.zhida.jiyangcenter.utils.glideImage
import com.zhida.jiyangcenter.utils.request
import com.zhida.jiyangcenter.widget.imagewatcher.GlideSimpleLoader
import com.zhida.jiyangcenter.widget.imagewatcher.ImageWatcherHelper
import kotlinx.android.synthetic.main.activity_medical_details.*

@TitleBar(false)
@ContentLayout(R.layout.activity_medical_details)
class MedicalDeatilsActivity : BaseActivity() {
    val pictureAdapter = PictureAdapter()
    private var iwHelper: ImageWatcherHelper? = null

    override fun initConfig(savedInstanceState: Bundle?) {
        title_bar.title("服务详情").navigationIcon(R.drawable.iv_back_fill) {
            finish()
        }
        recycler_view.layoutManager = LinearLayoutManager(context)
        recycler_view.addItemDecoration(
            HorizontalDividerItemDecoration.Builder(context).colorResId(R.color.colorBack)
                .sizeResId(R.dimen.dividerBig).build()
        )
        recycler_view.adapter = pictureAdapter
        iwHelper =
            ImageWatcherHelper.with(activity, GlideSimpleLoader()) // 一般来讲， ImageWatcher 需要占据全屏的位置
                .setErrorImageRes(R.drawable.error_picture)
        pictureAdapter.setOnItemClickListener { _, view, position ->
            val mappingViews = SparseArray<AppCompatImageView>()
            mappingViews.put(position, view.findViewById(R.id.iv_image))
            iwHelper?.show(
                view.findViewById(R.id.iv_image),
                mappingViews,
                iwHelper?.convert(pictureAdapter.data)
            )
        }
        getData()
    }

    private fun getData() {
        lifecycleScope.request({
            APIZaxiang.service().doctorServeInfo(intent.getStringExtra("uuid") ?: "")
        }, object : BaseAPI<DoctorServeInfoEntity>() {
            override fun onSuccess(t: DoctorServeInfoEntity?, state: Int, msg: String) {
                spinner_yiyuan.text = t?.hospital
                spinner_keshi.text = t?.department
                tv_time.text = t?.time
                edit_name.text = t?.name
                edit_cardID.text = t?.id_number
                edit_phone.text = t?.phone
                edit_opinion.text = t?.content
                pictureAdapter.setNewData(t?.picture)
            }

            override fun onError(e: APIException) {
                tipFail(e.eMessage)
            }
        })
    }

    class PictureAdapter :
        BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_seller_discuss_picture) {
        override fun convert(helper: BaseViewHolder, item: String?) {
            val imageView = helper.getView<AppCompatImageView>(R.id.iv_image)
            glideImage(imageView, item ?: "", R.color.colorBack)
        }
    }
}